package xyz.hubery.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.hubery.domain.SysRole;
import xyz.hubery.model.R;
import xyz.hubery.service.SysRoleService;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@RestController
@Api(tags = "角色管理")
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping
    @ApiOperation(value = "按条件分页查询角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "分页条件"),
            @ApiImplicitParam(name = "role",value = "查询条件")
    })
    @PreAuthorize("hasAuthority('sys_role_query')")
    public R<Page<SysRole>> findByPage(Page<SysRole> page,SysRole role) {
        // 构建条件查询对象
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasText(role.getCode()),SysRole::getCode,role.getCode());
        wrapper.like(StringUtils.hasText(role.getName()),SysRole::getName,role.getName());
        wrapper.eq(role.getStatus() != null,SysRole::getStatus,role.getStatus());

        page.addOrder(OrderItem.desc("last_update_time"));
        // 分页查询
        Page<SysRole> rolePage = sysRoleService.page(page, wrapper);
        return R.ok(rolePage);
    }


    @ApiOperation(value = "新增角色")
    @ApiImplicitParam(name = "role",value = "角色json数据")
    @PostMapping
    @PreAuthorize("hasAuthority('sys_role_create')")
    public R addRole(@RequestBody @Validated SysRole role) {
        boolean save = sysRoleService.save(role);
        if(save){
            return R.ok();
        }
        return R.fail();
    }


    @ApiOperation(value = "删除角色")
    @ApiImplicitParam(name = "ids",value = "角色id列表")
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('sys_role_delete')")
    public R delRole(@RequestBody List<Long> ids) {
        if(ids == null || ids.isEmpty()){
            return R.fail("删除数据不能为空");
        }
        boolean b = sysRoleService.removeByIds(ids);
        if(b) {
            return R.ok();
        }
        return R.fail();
    }

    @GetMapping("/{roleId}")
    @PreAuthorize("hasAuthority('sys_role_query')")
    @ApiOperation(value = "查询角色详情")
    @ApiImplicitParam(name = "roleId",value = "角色id")
    public R<SysRole> getDetails(@PathVariable("roleId")Long roleId){
        SysRole role = sysRoleService.getById(roleId);
        return R.ok(role);
    }


}
